package com.tan.steamship.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tan.steamship.common.Result;
import com.tan.steamship.service.OrdersService;
import com.tan.steamship.vo.CartVo;
import com.tan.steamship.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 订单
 * @author: longshifu
 * @time: 2025/3/11 18:50
 */
@RestController
@RequestMapping("/order/v1")
public class OrderController {
    @Autowired
    private OrdersService ordersService;


    /**
     * 获取订单列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/list/{userId}")
    public Result getOrder(@PathVariable(value = "userId",required = false) Integer userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return Result.error(1001, "用户未登录");
        }
//        Integer userId = (Integer) redisTemplate.opsForHash().get(cookie, "userId");
        List<List<OrderVo>> orders = ordersService.getOrderByUserId(userId);
        return Result.success(orders);
    }

    @PostMapping("/add/{userId}")
    public Result addOrder(@RequestBody List<CartVo> cartVoList, @PathVariable(value = "userId",required = false) Integer userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return Result.error(1001, "用户未登录");
        }
        ordersService.addOrder(cartVoList, userId);
        return Result.success("下单成功");
    }


}
